package com.esp.soccertutor.uc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.esp.soccertutor.R;
import com.esp.soccertutor.android.fbreader.FBReader;

import com.esp.soccertutor.util.Pref;

public class CustomMenu {

	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private PopupWindow mPopupWindow = null;
	private boolean mIsShowing = false;
	
	private Activity objActivity;
	
	public boolean isShowing() { return mIsShowing; }
	
	public CustomMenu(Context context, LayoutInflater lo) {
		mContext = context;
		mLayoutInflater = lo;
	}

	public void setActivity(Activity objActivity)
	{
		this.objActivity = objActivity;
	}
	
	public synchronized void show(View v) {
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		View mView= mLayoutInflater.inflate(R.layout.custom_menu, null);
		
		int[] btn = new int[]{R.id.btnLibrary, R.id.btnGetBook, R.id.btnNowReading, R.id.btnInfo};
		for(int ID : btn)
		{
			Button bt = (Button) mView.findViewById(ID);
			bt.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent intent = null;
					switch (v.getId()) {
						case R.id.btnLibrary:
//							intent = new Intent(mContext, BookListAll.class);
//							mContext.startActivity(intent);
							//objActivity.finish();
							break;
						case R.id.btnGetBook:
							
							break;
						case R.id.btnNowReading:
							//new Intent(mContext, ReadBook.class);
							if(Pref.getValue("BOOK_ID", null)!= null)
							{
								intent = new Intent(mContext, FBReader.class);
								mContext.startActivity(intent);
								//objActivity.finish();
							}
							else
								Toast.makeText(mContext, "Please, select the book !!", Toast.LENGTH_LONG).show();
							break;
						case R.id.btnInfo:
			
							break;
				
						default:
							break;
					}
					hide();
					
				}
			});
		}
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        mPopupWindow.setWidth(display.getWidth());
        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}
}
